package com.bmadmin.controller.sys;

import com.bmadmin.domain.LayUiDataTablePOJO;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.SysRolesPOJO;
import com.bmadmin.service.SysRolesService;
import com.bmadmin.util.BMUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by SEELE on 2017/12/5.
 */
@RestController
@RequestMapping("/roles")
public class SysRolesController {
    private Logger logger = LogManager.getLogger(this.getClass().getName());

    @Autowired
    SysRolesService sysRolesService;


    /**
     * 角色列表
     * @param sysRolesPOJO
     * @return
     */
    @GetMapping("/list")
    public LayUiDataTablePOJO getRoles(SysRolesPOJO sysRolesPOJO){
        LayUiDataTablePOJO result = new LayUiDataTablePOJO();
        result =sysRolesService.getRolesList(sysRolesPOJO);
        return result;
    }

    /**
     * 新增角色
     */
    @PostMapping(value = "/insert",produces = "application/json")
    public NormalResult insertRoles(@RequestBody SysRolesPOJO sysRolesPOJO){
        NormalResult result = new NormalResult();
        if(BMUtil.isNullOrEmpty(sysRolesPOJO)){
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }

        if(BMUtil.isNullOrEmpty(sysRolesPOJO.getR_name())){
            result.setCode("9999");
            result.setMsg("传入角色名称不得为空");
            return result;
        }

        result = sysRolesService.insertRole(sysRolesPOJO);
        return result;
    }

    /**
     * 删除角色
     */
    @PostMapping("/delete")
    public NormalResult deleteRoles(SysRolesPOJO sysRolesPOJO){
        NormalResult result = new NormalResult();
        if(BMUtil.isNullOrEmpty(sysRolesPOJO)){
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }

        if(BMUtil.isNullOrEmpty(sysRolesPOJO.getR_id())){
            result.setCode("9999");
            result.setMsg("传入角色id 不得为空");
            return result;
        }

        result = sysRolesService.deleteRole(sysRolesPOJO);

        return result;
    }

    /**
     * 修改角色
     */
    @PostMapping(value = "/update",produces = "application/json")
    public NormalResult updateRoles(@RequestBody SysRolesPOJO sysRolesPOJO){
        NormalResult result = new NormalResult();

        if(BMUtil.isNullOrEmpty(sysRolesPOJO)){
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }

        if(BMUtil.isNullOrEmpty(sysRolesPOJO.getR_name())){
            result.setCode("9999");
            result.setMsg("传入角色名称不得为空");
            return result;
        }

        if(BMUtil.isNullOrEmpty(sysRolesPOJO.getR_id())){
            result.setCode("9999");
            result.setMsg("传入角色id 不得为空");
            return result;
        }

        result = sysRolesService.updateRole(sysRolesPOJO);

        return result;
    }

    /**
     * 根据角色id 查询角色信息，包含权限树
     */
    @GetMapping("/getRoleInfoByIdIncludePermissionsTree")
    public NormalResult getRoleInfoByIdIncludePermissionsTree(SysRolesPOJO sysRolesPOJO){
        NormalResult result = new NormalResult();

        result =sysRolesService.findRoleByIdIncludePermissionTree(sysRolesPOJO);

        return result;
    }

    /**
     * 获取空权限树
     */
    @GetMapping("/getFreePermissionsTree")
    public NormalResult getFreePermissionsTree(){
        NormalResult result = new NormalResult();
        result = sysRolesService.getFreePermissionTree();
        return result;
    }

}
